/**
 * @require /static/widege/admin/popup/popup.js
 */
(function($) {
    var $element,
        $rightContent = $(".rightContent"),
        popupContent1 = __inline('/page/admin/operate/right/menuMessage1.tpl'),
        $rPopupOne = $.popup('添加模块', popupContent1),
        $deleteDom = $('<div class="deleteDom">删除</div>'),
        $rightBox = $(".rightBox"),
        $rightHtml = $(".rightHtml"),
        $leftMenu;
    $rPopupOne.css({
        "z-index": "1000"
    })
    $.PMS.dMenuMssageForm.index.init();
    var index = {
        init: function() {
            this.bindEvent();
            this.findRightMsg();
        },
        bindEvent: function() {
            var $self = this;
            $rightBox.on("contextmenu", function(e) {
                if ($(e.target) === $rightBox) {
                    return;
                };
                e.stopPropagation();
                switch (e.which) {
                    case 1:
                        break;
                    case 2:
                        break;
                    case 3:
                        if (jQuery.inArray(e.target, $(".canMove")) > 0 || $(e.target).parents(".canMove").length > 0) {
                            $element = (jQuery.inArray(e.target, $(".canMove")) > 0) ? $(e.target) : $(e.target).parents(".canMove");
                            $self.moveElement({
                                element: $element,
                                clientX: e.clientX,
                                clientY: e.clientY,
                            });

                        } else {
                            $rPopupOne.show('slow');
                        }
                        return false;
                        break;
                }
            });
            $(document).on("click", function() {
                $(".deleteDom").hide('slow');
            })
            $rightBox.on("mouseup", function(e) {
                if ($(e.target) === $rightBox) {
                    return;
                };
                if (jQuery.inArray(e.target, $(".canMove")) > 0) {
                    $(e.target).one("transitionend", function() {
                        $.communication($.leftMenu);
                    })
                } else if ($(e.target).parents(".canMove").length > 0) {
                    $(e.target).parents(".canMove").one("transitionend", function() {
                        $.communication($.leftMenu);
                    })
                }
            });
        },
        findRightMsg: function() {
            $(".menu-wrap").bind("findRightMsg", function(e, leftMenu) {
                $.initializeRight(leftMenu);
                $.leftMenu = leftMenu;
                console.log(leftMenu);
            })
        },
        moveElement: function(obj) {
            var ele = obj.element,
                clientX = obj.clientX,
                clientY = obj.clientY;
            if ($(".deleteDom").length === 0) {
                $rightBox.append($deleteDom);
            }
            $(".deleteDom").css({
                "top": (clientY - 95) + "px",
                "left": (clientX - 140) + "px"
            });
            $(".deleteDom").show();
            $(".deleteDom").on("click", function(event) {
                $(".deleteDom").hide();
                if (ele.next().length === 0) {
                    ele.remove();
                    $('.gridly').gridly();
                    $.communication($.leftMenu);
                } else {
                    ele.next().one("transitionend", function() {
                        $.communication($.leftMenu);
                    })
                    ele.remove();
                    $('.gridly').gridly();

                }
            });
        }
    };
    index.init();

})(jQuery);